package ani.trainning.cacobatph;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class ThreadDac {



	private final Handler mHandler;
	private final Context context;

	String user;
	String pass;

	// constructor
	public ThreadDac(Handler handler, Context context,String _user, String _pass) {
		mHandler = handler;
		this.context = context;
		this.user=_user;
		this.pass=_pass;
	}


	public Thread createThread() {
		return new Thread() {
			@Override
			public void run() {
				// creation du message.
				try {

					String response=cacoAuthentification(context, user,pass);

					// Recuperation de la reponse et l'envoi au handler.
					Message msg=new Message();
					msg.what = 1;
					msg.obj = response;
					mHandler.sendMessage(msg);

				} catch (Exception e) {
					Log.e("thread exeption : ", ""+e.getMessage());
					mHandler.sendEmptyMessage(0);
				}

			}
		};
	}
	public String cacoAuthentification(Context context, String userName, String passWord) {
		try {
			SoapObject request = new SoapObject(
					context.getResources().getString(R.string.NAMESPACE),
					context.getResources().getString(R.string.DAC_METHOD));



			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.dotNet = true;
			envelope.setOutputSoapObject(request);

			envelope.headerOut = new Element[1];
			envelope.headerOut[0] = buildAuthHeader(context, userName, passWord);

			HttpTransportSE androidHttpTransport = new HttpTransportSE(context.getResources().getString(R.string.URL));
			androidHttpTransport.call(context.getResources().getString(R.string.NAMESPACE)
					+ context.getResources().getString(R.string.DAC_METHOD), envelope, null);

			String result = String.valueOf(envelope.getResponse());
			Log.i("chargement", "result : " + result);
			return result;

		} catch (SoapFault sf) {
			Log.i("chargement", "sf exception : " + sf.faultstring);
			return "Soap fault : " + sf.faultstring;
		} catch (Exception e) {
			Log.i("chargement", "execption : " + e.getMessage());
			return "Exception : " + e.getMessage();
		}
	}

	public Element buildAuthHeader(Context context, String userName, String passWord) {
		Element h = new Element().createElement(context.getResources().getString(R.string.NAMESPACE), "cacoServiceHeader");

		Element Username = new Element().createElement(context.getResources().getString(R.string.NAMESPACE), "Username");
		Username.addChild(Node.TEXT, userName);
		h.addChild(Node.ELEMENT, Username);

		Element Password = new Element().createElement(context.getResources().getString(R.string.NAMESPACE), "Password");
		Password.addChild(Node.TEXT, passWord);
		h.addChild(Node.ELEMENT, Password);

		Element wsAccessUserName = new Element().createElement(context.getResources().getString(R.string.NAMESPACE), "wsAccessUserName");
		wsAccessUserName.addChild(Node.TEXT, "animapp");
		h.addChild(Node.ELEMENT, wsAccessUserName);

		Element wsAccessPassword = new Element().createElement(context.getResources().getString(R.string.NAMESPACE), "wsAccessPassword");
		wsAccessPassword.addChild(Node.TEXT, "ff3d10ac-a655-4da5-93f4-525074ec4ba1");
		h.addChild(Node.ELEMENT, wsAccessPassword);
		return h;
	}

}